from flask import Flask, render_template
from flask_redis import FlaskRedis

from Model import models
from View import user, search, data
from flask_cors import CORS

from conf import redis_url

app = Flask(__name__)
CORS(app, supports_credentials=True)

app.config['REDIS_URL'] = redis_url
redis = FlaskRedis(app)

app.register_blueprint(user.user_blue)
app.register_blueprint(search.search_blue)
app.register_blueprint(data.data_blue)
# 是否追踪数据库修改(开启后会触发一些钩子函数)  一般不开启, 会影响性能
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# 是否显示底层执行的SQL语句
app.config['SQLALCHEMY_ECHO'] = True
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
app.config["JSON_AS_ASCII"] = False
# 数据库连接
app.config['SQLALCHEMY_DATABASE_URI'] = models.connect()

# 分步加载，在这里初始化Flask-SQLAlchemy
models.db.init_app(app)
with app.app_context():
    models.db.create_all()  # 创建所有表。如果表不存在，那么会自动创建


@app.get('/')
def index():
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)
